package com.bj58.ecdata.calc.dbservice.crontab;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.bj58.ecdata.calc.dbservice.dao.BaseDao;
import com.bj58.ecdata.calc.dbservice.dao.ClickDao;
import com.bj58.ecdata.calc.dbservice.dao.DisplayDao;
import com.bj58.ecdata.calc.dbservice.dao.InfoDao;
import com.bj58.ecdata.calc.dbservice.dao.ResumeAddDao;
import com.bj58.ecdata.calc.dbservice.dao.TrackDao;
import com.bj58.ecdata.util.DateUtils;
import com.google.common.collect.Lists;

/**
 * 定时任务，每天定时清理mysql数据
 * @author zhaoxiang
 */
@Component
public class ClearMysqlData implements InitializingBean {
	private List<BaseDao<?>> baseDaos = Lists.newArrayList();
	@Autowired
	private ClickDao clickDao;
	@Autowired
	private TrackDao trackDao;
	@Autowired
	private DisplayDao displayDao;
	@Autowired
	private InfoDao infoDao;
	@Autowired
	private ResumeAddDao resumeAddDao;
	private static final int RETENTION_DAYS = 15;
	private Logger log = LoggerFactory.getLogger(ClearMysqlData.class);

	@Override
	public void afterPropertiesSet() throws Exception {

		baseDaos.add(trackDao);
		baseDaos.add(displayDao);
		baseDaos.add(clickDao);
		baseDaos.add(infoDao);
		baseDaos.add(resumeAddDao);
	}


	@Scheduled(cron = "00 00 3 * * ?")
	public void clearData() {
		Date date =  DateUtils.addDays(new Date(), 0 - RETENTION_DAYS);
		clearData(date);
	}
	
	private void clearData(Date date) {
		for (BaseDao<?> baseDao : baseDaos) {
			String daoName = baseDao.getClass().getSimpleName();
			try {
				int result = baseDao.clearData(date);
				log.info("crontab: clear mysql {} before {}.", daoName, date, result);
			} catch (Exception e) {
				log.error("clear mysql error : " + daoName, e);
			}
		}
	}
	
/*	
	@Scheduled(cron = "20 50 3 * * ?")
	public void clearData1() {
		Date date = DateUtils.truncate(new Date(), Calendar.DATE);
		date = DateUtils.addDays(date, 0 - RETENTION_DAYS);
		date = DateUtils.addHours(date, 10);// 0~9
		clearData(date);
	}

	@Scheduled(cron = "20 05 4 * * ?")
	public void clearData2() {
		Date date = DateUtils.truncate(new Date(), Calendar.DATE);
		date = DateUtils.addDays(date, 0 - RETENTION_DAYS);
		date = DateUtils.addHours(date, 13);// 10~12
		clearData(date);
	}

	@Scheduled(cron = "20 20 4 * * ?")
	public void clearData3() {
		Date date = DateUtils.truncate(new Date(), Calendar.DATE);
		date = DateUtils.addDays(date, 0 - RETENTION_DAYS);
		date = DateUtils.addHours(date, 16);// 13~15
		clearData(date);
	}

	@Scheduled(cron = "20 35 4 * * ?")
	public void clearData4() {
		Date date = DateUtils.truncate(new Date(), Calendar.DATE);
		date = DateUtils.addDays(date, 0 - RETENTION_DAYS);
		date = DateUtils.addHours(date, 20);// 16~19
		clearData(date);
	}

	@Scheduled(cron = "20 50 4 * * ?")
	public void clearData5() {
		Date date = DateUtils.truncate(new Date(), Calendar.DATE);
		date = DateUtils.addDays(date, 0 - RETENTION_DAYS + 1);// 18~24
		clearData(date);
	}

	*/

}
